import { onload } from '../utils/index'
import tracker from '../utils/tracker'

export default function blankScreen() {
  let wrapperElements = ['html', 'body', '#container', '.content']
  let emptyPoints = 0

  function getSelector(element) {
    if (element.id) {
      return `#${element.id}`
    } else if (element.className && typeof element.className === 'string') {
      return `.${element.className}`
    } else {
      return element.nodeName.toLowerCase()
    }
  }

  function isWrapper(element) {
    let selector = getSelector(element)
    if (wrapperElements.indexOf(selector) !== -1) {
      emptyPoints++
    }
  }

  onload(function () {
    for (let i = 0; i < 9; i++) {
      let xElements = document.elementFromPoint(
        (window.innerWidth * i) / 10,
        window.innerHeight / 2
      )
      let yElements = document.elementFromPoint(
        (window.innerHeight * i) / 10,
        window.innerWidth / 2
      )
      isWrapper(xElements)
      isWrapper(yElements)
    }

    if (emptyPoints > 16) {
      let centerElements = document.elementFromPoint(
        window.innerWidth / 2,
        window.innerHeight / 2
      )

      tracker.send({
        kind: 'stability',
        type: 'blank',
        emptyPoints,
        screen: window.screen.width + 'X' + window.screen.height,
        viewPoint: window.innerWidth + 'X' + window.innerHeight,
        selector: getSelector(centerElements)
      })
    }
  })
}
